package com.yys.apm.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yys.apm.entity.AppData;
import com.yys.apm.entity.AppDataExt;
import com.yys.apm.mapper.AppDataExtMapper;
import com.yys.apm.mapper.AppDataMapper;
import com.yys.apm.service.IDataReceiveService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.time.LocalDateTime;

/**
 * @author Yingsheng Ye
 */
@Service(value = "collectService")
public class CollectServiceImpl implements IDataReceiveService {

    @Resource
    private AppDataMapper appDataInfoMapper;

    @Resource
    private AppDataExtMapper appDataInfoExtMapper;

    @Override
    public void dataProcess(String data) {
        JSONObject jsonObject = JSON.parseObject(data);
        AppData appDataInfo = new AppData();
        appDataInfo.setAppId(jsonObject.getString("appId"));
        appDataInfo.setTraceId(jsonObject.getString("traceId"));
        appDataInfo.setParentId(jsonObject.getString("parentId"));
        appDataInfo.setSpanId(jsonObject.getString("spanId"));
        appDataInfo.setClassName(jsonObject.getString("className"));
        appDataInfo.setMethodName(jsonObject.getString("methodName"));
        appDataInfo.setHostIp(jsonObject.getString("hostIp"));
        appDataInfo.setHostName(jsonObject.getString("hostName"));
        appDataInfo.setRequestUrl(jsonObject.getString("requestUrl"));
        appDataInfo.setCostTime(jsonObject.getLong("costTime"));
        appDataInfo.setStartTime(jsonObject.getLong("startTime"));
        appDataInfo.setEndTime(jsonObject.getLong("endTime"));
        appDataInfo.setType(jsonObject.getString("type"));
        appDataInfo.setMsg(jsonObject.getString("msg"));
        appDataInfo.setVisitUserId(jsonObject.getString("visitUserId"));
        appDataInfo.setStatus(jsonObject.getInteger("status"));
        appDataInfo.setCreateTime(LocalDateTime.now());
        appDataInfoMapper.insert(appDataInfo);
        String[] arr = getFieldName(appDataInfo);
        AppDataExt appDataInfoExt;
        for (JSONObject.Entry entry : jsonObject.entrySet()) {
            boolean ignoreFlag = false;
            for (String field : arr) {
                if (entry.getKey().equals(field)) {
                    ignoreFlag = true;
                    break;
                }
            }
            if (!ignoreFlag) {
                appDataInfoExt = new AppDataExt();
                appDataInfoExt.setDataId(appDataInfo.getDataId());
                appDataInfoExt.setExtLabel(String.valueOf(entry.getKey()));
                appDataInfoExt.setExtValue(String.valueOf(entry.getValue()));
                appDataInfoExt.setCreateTime(LocalDateTime.now());
                appDataInfoExtMapper.insert(appDataInfoExt);
            }
        }
    }

    private String[] getFieldName(Object o) {
        Field[] fields = o.getClass().getDeclaredFields();
        String[] fieldNames = new String[fields.length];
        for (int i = 0; i < fields.length; i++) {
            fieldNames[i] = fields[i].getName();
        }
        return fieldNames;
    }

}
